home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8493 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  63 lines

  1. Newsgroups: comp.lang.c
  2. Path: in2.uu.net!world!mv!usenet
  3. From: ENGR@GSSI.MV.COM (Michael Furman)
  4. Subject: Re: Calling a Function Twice from a printf statement.
  5. Message-ID: <DnrALq.758@mv.mv.com>
  6. Mime-Version: 1.0
  7. Organization: GSSI
  8. Date: Mon, 4 Mar 1996 18:41:50 GMT
  9. References: <4he6q9$6r6@newsbf02.news.aol.com>
  10. X-Newsreader: WinVN 0.93.10
  11. X-Nntp-Posting-Host: gssi.mv.com
  12.  
  13. In article <4he6q9$6r6@newsbf02.news.aol.com>, razine@aol.com says...
  14. >
  15. >I am trying to call a function twice from my program on the same printf
  16. >line and have run into a small problem.  i was wondering if someone can
  17. >explain how to fix it.
  18. >
  19. >
  20. >void main(void) {
  21. >
  22. >   printf("First Number is %d , the Second s %d \n",num(0),num(1));
  23. >
  24. >}
  25. >
  26. >int num(int index) {
  27. >   int return_number;
  28. >   
  29. >   switch(index) {
  30. >       case 0 : return_number=0;
  31. >       case 1 : return_number=1;
  32. >                       }
  33. >   return(return_number);
  34. >}
  35. >
  36. >
  37. >Now with this function, the printf line will display 0 for the first one
  38. >and garbage for the second one..  I then thought if I made the variable
  39. >return_number a static variable it would have fixed it but then when i
  40. >made the change the printf line would give me zero for both of them.  Any
  41. >ideas?
  42.  
  43. Your program as you posted it will print "1" for both values because your
  44. forget break statement after assigning "0" to return_number - it will
  45. immediately reassigne "1" to this variable.
  46. Also - it is a bad practice to use "void main()" because it is not supported
  47. by standard (thow most compillers will accept it).
  48.  
  49.  
  50. >
  51. >Darrell
  52. >
  53.  
  54. -- 
  55. <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
  56. ---------------------------------------------------------------
  57. Michael Furman,                       (603)893-1109
  58. Geophysical Survey Systems, Inc.  fax:(603)889-3984
  59. 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com 
  60. North Salem, NH 03073-0097            71543.1334@compuserve.com
  61. ---------------------------------------------------------------
  62.  
  63.